package com.lyy.lottery.common.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//@Component
//public class SMSUtil {
//    private static final Logger logger = LoggerFactory.getLogger(SMSUtil.class);
//
//    @Value(value = "${sms.sign-name}")
//    private String signName;
//    @Value(value = "${sms.access-key-id}")
//    private String accessKeyId;
//    @Value(value = "${sms.access-key-secret}")
//    private String accessKeySecret;
//
//    //发送短信(模板号，手机号，模板参数{"key":"value"})
//    public void sendMessage(String templateCode, String phoneNumbers, String templateParam) {
//        try {
//            Client client = createClient();
//            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new SendSmsRequest()
//                    .setSignName(signName)
//                    .setTemplateCode(templateCode)
//                    .setPhoneNumbers(phoneNumbers)
//                    .setTemplateParam(templateParam);
//            RuntimeOptions runtime = new RuntimeOptions();
//            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
//            if (null != response.getBody()
//                    && null != response.getBody().getMessage()
//                    && "OK".equals(response.getBody().getMessage())) {
//                logger.info("向{}发送信息成功，templateCode={}", phoneNumbers, templateCode);
//                return;
//            }
//            logger.error("向{}发送信息失败，templateCode={}，失败原因：{}",
//                    phoneNumbers, templateCode, response.getBody().getMessage());
//        } catch (TeaException error) {
//            logger.error("向{}发送信息失败，templateCode={}", phoneNumbers, templateCode, error);
//        } catch (Exception _error) {
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            logger.error("向{}发送信息失败，templateCode={}", phoneNumbers, templateCode, error);
//        }
//    }
//
//    //使用AK&SK初始化账号Client
//    private Client createClient() throws Exception {
//        Config config = new Config()
//                .setAccessKeyId(accessKeyId)
//                .setAccessKeySecret(accessKeySecret);
//        config.endpoint = "dysmsapi.aliyuncs.com";
//        return new Client(config);
//    }
//}
